<?php

class Admin_Form_Professor extends Zend_Form
{
    public function init() {
   	    
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        $urlbase = $front->getBaseUrl();
                
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $this->setName('form_pdr');
        
        Zend_Loader::loadClass('dominio','library');
        
        $id = $front->getRequest()->getParam('id', 0);
        
        if($id > 0){
           $query = 'SELECT SQL_CACHE * FROM PROFESSOR WHERE PRS_ID = ' . $id;
           $data = $db->fetchRow($query);
        }
        
        $el = new Zend_Form_Element_Hidden('PRS_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Hidden('USU_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Text('PRS_NOM');
        $el->setLabel('Nome')->setRequired(true);
        $el->setAttrib('required','');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRS_TEL');
        $el->setLabel('Telefone')->setRequired(true);
        $el->setAttrib('required','');
        $el->setAttrib('class','telefone');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRS_EML');
        $el->setLabel('E-mail')->setRequired(true);
        $el->setAttrib('required','');
        $el->setAttrib('class','email');
        $this->addElement($el);
        
        $query = 'SELECT SQL_CACHE DCP_ID, DCP_NOM FROM DISCIPLINA WHERE DCP_STA = 1 ORDER BY DCP_NOM';
        $row = $db->fetchAll($query); 
        foreach($row as $r) $arr[$r['DCP_ID']] = $r['DCP_NOM']; 
        
        $el = new Zend_Form_Element_Select('DCP_ID',array('onchange' => ''));
        $el->setLabel('Disciplina')->setRequired(true);
        $el->addMultiOptions($arr);
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRS_LOG');
        $el->setLabel('Login')->setRequired(true);
        $el->setAttrib('required','');
        //if($id > 0) $el->setAttrib('disabled','disabled'); 
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Password('PRS_SNH');
        $el->setLabel('Senha');
        if($id == 0) $el->setAttrib('required','');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Password('PRS_SNH2');
        $el->setLabel('Repita Senha');
        if($id == 0) $el->setAttrib('required','');
        $this->addElement($el);
        
        $arr_set = array();
        foreach(explode(',',$data['CBS_ID']) as $arr_ex) $arr_set[$arr_ex] = $arr_ex;
        
        $htmli = '<select id="CBS_ID[]" name="CBS_ID[]" class="chosen-select CBS_ID" multiple="multiple" style="width:500px;" '.$disabled.'>';
            
        $arr = array('0'=>'Todos Cursos');
        $query = 'SELECT SQL_CACHE CBS_ID, CBS_NOM FROM CURSOBASE WHERE CBS_STA = 1 ORDER BY CBS_ORD ASC';
        $row = $db->fetchAll($query);
        foreach($row as $c){
            $selected = array_search($c['CBS_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$c['CBS_ID'].'">'.$c['CBS_NOM'].'</option>'; 
        }
        $htmli .= '</select>';
        $style = $data['CBS_ID'] > '' ? '':'';
        //$html = '<div><label><input type="radio" '.$checked.' class="seg_ck" name="destino" value="c" '.$disabled.'> Cursos<label>';
        $html = '<div id="cursos" class="destino" '.$style.'>'.$htmli.'</div></div>';

        $el = new Zend_Form_Element('CBS_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Cursos');
        $this->addElement($el);
        
        $arr_set = array();
        foreach(explode(',',$data['CUR_ID']) as $arr_ex) $arr_set[$arr_ex] = $arr_ex;
        
        $htmli = '<select id="CUR_ID[]" name="CUR_ID[]" class="chosen-select CUR_ID" multiple="multiple" style="width:500px;" '.$disabled.'>';
        
        if($data['CBS_ID'] > ''){
            $arr = array('0'=>'Todas Turmas');
            $query = 'SELECT SQL_CACHE c.CUR_ID,c.TUR_ID,c.CUR_ANO,c.CUR_SEM,t.NRT_NOM,cb.CBS_NOM '
                    .' FROM CURSO c, NUMEROTURMA t, CURSOBASE cb '
                    .' WHERE c.CBS_ID in('.$data['CBS_ID'].') AND c.CUR_TRM = t.NRT_ID '
                    .' AND c.CBS_ID = cb.CBS_ID '
                    .' AND c.CUR_STA = 1 and c.CUR_ANO >= "'.date('Y').'"'
                    .' ORDER BY cb.CBS_NOM ASC, t.NRT_NOM ASC, c.CUR_ANO DESC';
            $row = $db->fetchAll($query);
            foreach($row as $c){
                $selected = array_search($c['CUR_ID'], $arr_set) > '' ? 'selected=""':'';            
                $htmli .= '<option '.$selected.' value="'.$c['CUR_ID'].'">'. $c['CBS_NOM'].' '.dominio::getTurno($c['TUR_ID']).' '.$c['NRT_NOM'].' '.$c['CUR_ANO'].' '.dominio::getSemestres($c['CUR_SEM']).'</option>'; 
            }
        }
        $htmli .= '</select>';
        $style = $data['CUR_ID'] > '' ? '':'';
        //$html = '<div><label><input type="radio" '.$checked.' class="seg_ck" name="destino" value="g" '.$disabled.'>Grupos<label>';
        $html = '<div id="grupos" class="destino" '.$style.'>'.$htmli.'</div></div>';

        $el = new Zend_Form_Element('CUR_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Turmas');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('PRS_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
        
        $btn = new Zend_Form_Element_Submit('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setLabel('Salvar');
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        
    }

}

